package com.base.engine.gui;

import java.util.ArrayList;

import org.lwjgl.opengl.Display;

import com.base.engine.MADEngine;
import com.base.engine.font.MADFontManager;
import com.base.engine.font.MADFontRenderer;
import com.base.engine.game.MADGame;
import com.base.engine.graphics.Color4f;
import com.base.engine.graphics.MADRenderer;

import static org.lwjgl.opengl.GL11.*; 


public class MADDebuggingScreen extends MADGamingInterface {

	static class MADErrorMessage{
		
		private String message; 
		private String errorCode ; 
		
		public MADErrorMessage(String msg, String error){
			setMessage(msg);
			setErrorCode(error);
		}

		public String getMessage() {
			return message;
		}

		public void setMessage(String message) {
			this.message = message;
		}

		public String getErrorCode() {
			return errorCode;
		}

		public void setErrorCode(String errorCode) {
			this.errorCode = errorCode;
		}
		
	}
	
	public static final int WIDTH = Display.getWidth() ; 
	public static final int HEIGHT = 300 ; 
	
	private ArrayList<MADErrorMessage> messages ; 
	private ArrayList<String> info ; 
	
	public MADDebuggingScreen(MADGame g, float left, float top, float width, float height) {
		super(g, left, top, width, height, "debugging");
		messages = new ArrayList<MADErrorMessage>();
		info = new ArrayList<String>();
		info.add(MADEngine.appTitle);
		info.add("Calculating frame rate .. ");
		info.add("Calculating memory ..."); 
	}
	
	public ArrayList<String> getInfo(){
		return info	; 
	}
	
	public void addMessage(String message, String error){
		if(!(message == null ) && !(message.equals("")))
			messages.add(new MADErrorMessage(message, error));
	}
	
	public void update(){
		super.update(); 
	}
	
	public void render(){
		if(super.isVisible()){
			glPushMatrix();  
				MADRenderer.renderRectangle_2f(vertices, new Color4f(0.0f,0.0f,0.0f,0.0f), GL_QUADS);
				int k = 0 ;
				for(String s: info){
					MADFontRenderer.renderText(getLeft()+10, getTop()+15*k, s,
							MADFontManager.getUFontByName(MADFontManager.DEBUGGING_TEXT)); 
					k++ ; 
				}
				glColor4f(0.9f,0.9f,0.9f,0.95f);
				glLineWidth(1); 
				glBegin(GL_LINES);
					glVertex2f(getLeft(),++k*15);
					glVertex2f(getLeft()+WIDTH,k*15);
				glEnd(); 
				int i = ++k ; 
				for(MADErrorMessage e: messages){
					if(maxCapacityReached(i+2,15)){
						clearScreen(); 
						i = ++k ; 
						break ; 
					}
					if(!(e == null)){
						MADFontRenderer.renderText(getLeft()+10, getTop()+15*i, e.getMessage(), 
								MADFontManager.getUFontByName(MADFontManager.DEBUGGING_TEXT));
						i++ ; 
						MADFontRenderer.renderText(getLeft()+20, getTop()+15*i, e.getErrorCode(), 
									MADFontManager.getUFontByName(MADFontManager.DEBUGGING_ERROR));
						i++ ; 
					}
				}
			glPopMatrix();
		}
	}
	
	public void clearScreen(){
		messages.clear();
	}
	
	private boolean maxCapacityReached(int amtDisplayedMessages, int avgLineHeight){
		return ((amtDisplayedMessages * avgLineHeight) >= HEIGHT) ; 
	}

}
